package com.neu.admin;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.neu.common.RequiresPermissionsDesc;
import com.neu.common.validator.Order;
import com.neu.common.validator.Sort;
import com.neu.domain.ShoppingGoods;
import com.neu.domain.ShoppingGroupon;
import com.neu.domain.ShoppingGrouponRules;
import com.neu.service.ShoppingGoodsService;
import com.neu.service.ShoppingGrouponRulesService;
import com.neu.service.ShoppingGrouponService;
import com.neu.util.ResponseUtil;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ApiIgnore
@RestController
@RequestMapping("/admin/groupon")
@Validated
public class AdminGrouponController {
    private final Log logger = LogFactory.getLog(AdminGrouponController.class);

    @Autowired
    private ShoppingGrouponRulesService rulesService;
    @Autowired
    private ShoppingGoodsService goodsService;
    @Autowired
    private ShoppingGrouponService grouponService;

    @ApiOperation(value="获取团购(List)分页数据", notes="分页查询团购信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "每页多少条数据", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "sort", value = "排序字段", required = true, dataType = "String"),
            @ApiImplicitParam(name = "order", value = "排序规则", required = true, dataType = "String"),
            @ApiImplicitParam(name = "grouponId", value = "团购Id", required = false, dataType = "String")
    })
    @RequiresPermissions("admin:groupon:read")
    @RequiresPermissionsDesc(menu = {"推广管理", "团购管理"}, button = "详情")
    @GetMapping("/listRecord")
    public Object listRecord(String grouponId,
                             @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer limit,
                             @Sort @RequestParam(defaultValue = "add_time") String sort,
                             @Order @RequestParam(defaultValue = "desc") String order) {
        List<ShoppingGroupon> grouponList = grouponService.querySelective(grouponId, page, limit, sort, order);

        List<Map<String, Object>> groupons = new ArrayList<>();
        for (ShoppingGroupon groupon : grouponList) {
            try {
                Map<String, Object> recordData = new HashMap<>();
                List<ShoppingGroupon> subGrouponList = grouponService.queryJoinRecord(groupon.getId());
                ShoppingGrouponRules rules = rulesService.queryById(groupon.getRulesId());
                ShoppingGoods goods = goodsService.findById(rules.getGoodsId());

                recordData.put("groupon", groupon);
                recordData.put("subGroupons", subGrouponList);
                recordData.put("rules", rules);
                recordData.put("goods", goods);

                groupons.add(recordData);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }

        return ResponseUtil.okList(groupons, grouponList);
    }

    @ApiOperation(value="获取团购(List)分页数据", notes="分页查询团购信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "每页多少条数据", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "sort", value = "排序字段", required = true, dataType = "String"),
            @ApiImplicitParam(name = "order", value = "排序规则", required = true, dataType = "String"),
            @ApiImplicitParam(name = "goodsId", value = "商品Id", required = false, dataType = "String")
    })
    @RequiresPermissions("admin:groupon:list")
    @RequiresPermissionsDesc(menu = {"推广管理", "团购管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String goodsId,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<ShoppingGrouponRules> rulesList = rulesService.querySelective(goodsId, page, limit, sort, order);
        return ResponseUtil.okList(rulesList);
    }
}